<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Config;

class TrainingMaterials extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $data=input();
        if($data['title']){
            $map['title']=array('like',"%{$data['title']}%");
        }
        if($this::$isdelete){
            $map['m.isdelete']=1;
        }else{
            $map['m.isdelete']=0;
        }
        if($data['video_title']){
            $map['video_title']=array('like',"%{$data['video_title']}%");
        }
        $listRows = $this->request->param('numPerPage') ? : Config::get("paginate.list_rows");
        $list = Db::name('training_materials')
            ->alias('m')
            ->join('driver_rank r', 'm.rank_id=r.id', 'left')
            ->field('r.*,m.*')
            ->where($map)
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        $this->view->assign('list',$list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());
        return $this->view->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        if($this->request->isAjax()){
            $data=input();
            unset($data['id']);
            if($data['video_img_id']){
                $data['video_img_id']=$data['video_img_id'][0];
            }

            $re=Db::name("TrainingMaterials")->insert($data);
            if($re){
                return ajax_return_adv("添加成功");
            }else{
                return ajax_return_error("添加失败");
            }
        }else{
            //获取等级列表
            $rank_list = get_ranks();

            $this->view->assign('rank_list',$rank_list);
            return $this->view->fetch('edit');
        }
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $data=input();
        $id=$data['id'];
        if($this->request->isAjax()){
            unset($data['id']);
            if($data['video_img_id']){
                $data['video_img_id']=$data['video_img_id'][0];
            }
            $re=Db::name("TrainingMaterials")->where('id',$id)->update($data);
            if($re!==false){
                return ajax_return_adv("修改成功");
            }else{
                return ajax_return_error("修改失败");
            }
        }else{
            $vo=Db::name("TrainingMaterials")
                ->alias('m')
                ->join('driver_rank r', 'm.rank_id=r.id', 'left')
                ->field('r.*,m.*')
                ->where('m.id',$id)->find();

            //获取等级列表
            $rank_list = get_ranks();

            $this->view->assign('vo',$vo);
            $this->view->assign('rank_list',$rank_list);
            return $this->view->fetch('edit');
        }
    }
}
